package com.fenghuajueli.module_find_the_difference.zhao;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.blankj.utilcode.util.ToastUtils;
import com.fenghuajueli.module_find_the_difference.entity.Zhaocha;
import com.fenghuajueli.module_find_the_difference.utils.FileUtil;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonParser;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class ZhaoActivityPresenter extends ZhaoActivityContract.Presenter {

    private static final String TAG = ZhaoActivityPresenter.class.getSimpleName();

    private ZhaoActivityContract.View view;

    private int guan = 0;

    public ZhaoActivityPresenter(Context context) {
        super(context);
    }

    @Override
    public void takeView(ZhaoActivityContract.View view, Bundle data) {
        this.view = view;
        if (data != null) {
            guan = data.getInt("guan");
        }
        init();
    }

    @Override
    public void dropView() {
        view = null;
    }

    @Override
    public void resumeView() {
    }

    @Override
    public void pauseView() {
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    }

    private void init() {
        Observable.timer(500, TimeUnit.MILLISECONDS)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Long aLong) {
                        if (view != null) {
                            view.showTishiCount(spHelper.getTishiCount());
                        }
                        List<Zhaocha> ret = new ArrayList<>();
                        String json = FileUtil.getFileText(context, "zhaocha2.json", "utf-8");

                        Gson gson = new Gson();
                        JsonParser jsonParser = new JsonParser();
                        JsonArray jsonElements = jsonParser.parse(json).getAsJsonArray();
                        for (int i = 0; i < jsonElements.size(); i++) {
                            Zhaocha zhaocha = gson.fromJson(jsonElements.get(i), Zhaocha.class);
                            ret.add(zhaocha);
                        }
                        /*try {
                            JSONArray array = new JSONArray(json);
                            for (int i = (guan - 1) * 2; i < array.length() && i < guan * 2; i++) {
                                JSONObject item = array.getJSONObject(i);
                                Zhaocha zhaocha = new Zhaocha();
                                zhaocha.image1 = item.getString("image1");
                                zhaocha.image2 = item.getString("image2");
                                JSONArray list = item.getJSONArray("list");
                                for (int j = 0; j < list.length(); j++) {
                                    JSONObject p = list.getJSONObject(j);
                                    zhaocha.list.add(new Point(p.getInt("x"), p.getInt("y")));
                                }
                                ret.add(zhaocha);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }*/
                        if (view != null) {
                            view.showData(ret, guan);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void next() {
        if (guan < 12) {
            guan++;
            init();
        } else {

            ToastUtils.showShort("已经是最后一关");
        }
    }

    @Override
    public void showTishi() {
        int c = spHelper.getTishiCount();
        if (c <= 0) {
            ToastUtils.showShort("青蛙数量不足");
        } else {
            c--;
            spHelper.setTishiCount(c);
            if (view != null) {
                view.showTishi();
            }
        }
    }
}
